<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>创建或修改文章</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../layui/css/layui.css" media="all">
</head>
<body>
  <form id="post_form" class="layui-form" action="" lay-filter="post_form" style="padding:20px;">
    <div class="layui-form-item">
      <label class="layui-form-label">文章标题</label>
      <div class="layui-input-inline">
        <input type="text" name="title" lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">文章内容</label>
      <div class="layui-input-inline" style="width:600px;">
          <textarea id="content" style="display: none;"></textarea>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">分类</label>
      <div class="layui-input-inline">
        <select name="category" lay-verify="required" id="categorySelect">
          <option value="">请选择分类</option>
        </select>
      </div>
    </div>

    <div class="layui-form-item">
      <div class="layui-input-block">
        <button class="layui-btn" lay-submit="" lay-filter="btn-submit">立即提交</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        <a class="layui-btn layui-btn-primary" href="javascript:history.go(-1);" style="margin-left:50px;">
        	返回上级
        </a>
      </div>
    </div>
  </form>

  <script src="../layui/layui.js"></script>  
  <script>
  layui.use(['form', 'layedit'], function(){
    var $ = layui.$, form = layui.form;

    var layedit = layui.layedit;
    var editorIndex = layedit.build('content'); //建立编辑器
    
    //监听提交
    form.on('submit(btn-submit)', function(data){
      // 把富文本编辑器里面的内容取出来，设置到提交的对象里面去
	  data.field.content = layedit.getContent(editorIndex);

      $.post("../post/create", data.field, function(response) {
    	  // 返回上一级
    	  window.history.go(-1);
      });  

      //提交 Ajax 成功后，关闭当前弹层并重载表格
      //$.ajax({});
      return false;
    });

    //自定义验证规则
//     form.verify({
//       title: function(value) {
//         if(value.length < 5) {
//           return '标题至少5个字符';
//         }
//       },
//       content: function(value) {
//         layedit.sync(editorIndex);
//       }
//     });
    
    //表单初始赋值
    form.val('post_form', {
      "category": "1"
    })
    
    // 页面加载完成之后，查询所有的分类信息，显示到下拉框
    $.get("../category/findAll", function(response) {
  	  var categories = response.data;
  	  
  	  // <option value='id'>name</option>
  	  for (var i = 0; i < categories.length; i++) {
  		  var item = categories[i];
  		  var option = "<option value='" + item.id + "'>" + item.name + "</option>";
  		  $('#categorySelect').append(option);
  	  }
  	  
  	  form.render('select');
    });
  })
  </script>
</body>
</html>